
import * as cc from "cc";
import { UIWindowBase } from '../../../framework/ui/UIWindowBase';
import { VMCenter } from '../../../framework/vm/VMCenter';
import { VMEntity } from '../../../framework/vm/VMEntity';
import { player } from "../../../userdata/UserModel";
const { ccclass } = cc._decorator;

/**
 * Predefined variables
 * Name = AddPointWindow
 * DateTime = Sun Mar 20 2022 16:55:18 GMT+0800 (中国标准时间)
 * Author = 272493431
 * FileBasename = AddPointWindow.ts
 * FileBasenameNoExtension = AddPointWindow
 * URL = db://assets/scripts/scene/VMDemo/windows/AddPointWindow.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
class ModelAddPoint {

    point:number = 0;//可以使用的技能点
    use:number = 0; //已经使用的技能点

    data = {
        atk:0,
        mat:0,
        def:0,
        mdf:0,
        spd:0,
        luk:0,
        cri:0 //暴击率
    }

    add = {
        atk:0,
        mat:0,
        def:0,
        mdf:0,
        spd:0,
        luk:0,
        cri:0 //暴击率
    }


}
@ccclass('AddPointWindow')
export class  AddPointWindow extends UIWindowBase implements IMVVM{
    _vmTag: string;
    _vmEntitys: VMEntity[];

    data: ModelAddPoint;

    constructor(){
        super();
        this.onCtor("prefabs/AddPointWindow");
    }

    onLoaded(): void {
        let self = this;
        this.data = new ModelAddPoint();
        this.onActive();
        VMCenter.add(this.data,this);
        
        let LabelTotal = this.find(">LabelTotal");
        let ButtonReset = this.find(">ButtonReset");
        let ButtonAuto = this.find(">ButtonAuto");
        let ButtonOk = this.find(">ButtonOk");
        let ButtonCancle = this.find(">ButtonCancle");

        let prop = this.find("prop");

        let _player = player;
        self.data.point = player.prop_point;

        VMCenter.Label(this,LabelTotal,{watchPath: ["*.use","player.prop_point"]});

        prop.children.forEach((node)=>{
            let prop = node.name;
            this.data.data[prop] = _player[prop];
            VMCenter.Label(this,node,{watchPath: [`*.data.${prop}`,`*.add.${prop}`]});
            VMCenter.Color(this,node,{watchPath: `*.add.${prop}`,formator(opts){
                return opts.newValue > 0 ? cc.color().fromHEX("#47d948") : cc.Color.WHITE;
            }});

            // 获取加减按钮
            let btn_sub = this.find("add/btn_sub",node);
            let btn_add = this.find("add/btn_add",node);
            
            // 减点
            VMCenter.ButtonClick(this,btn_sub,{watchPath: [`*.data.${prop}`,`*.add.${prop}`,`*.point`,`*.use`],operation(opts){
                let curVal = opts.currentValue;
                return curVal[1] > 0 ? [curVal[0] - 1,curVal[1] - 1,curVal[2] + 1,curVal[3] - 1] : curVal;
            }});
            // 加点
            VMCenter.ButtonClick(this,btn_add,{watchPath: [`*.data.${prop}`,`*.add.${prop}`,`*.point`,`*.use`],operation(opts){
                let curVal = opts.currentValue;
                return self.data.point > 0 ? [curVal[0] + 1,curVal[1] + 1,curVal[2] - 1,curVal[3] + 1] : curVal;
            }});
            
        });


        // 重置加点
        this.registeButtonClick(ButtonReset,()=>this.onClickResetPoint());
        this.registeButtonClick(ButtonAuto,()=>this.onClickAuto());
        
        this.registeButtonClick(ButtonOk,()=>this.onClickOk());
        this.registeButtonClick(ButtonCancle,()=>this.onClickCancel());   

    }

    onUnloaded(): void {
        this.onFreeze();
        VMCenter.remove(this);
    }


    /**
     * 重置加点
     */
    onClickResetPoint(){
        for (const key in this.data.data) {
            this.data.data[key] = player[key];
        }
        
        for (const key in this.data.add) {
            this.data.add[key] = 0;
        }
        
        this.data.use = 0;
        this.data.point = player.prop_point;
    }


    onClickAuto(){
        
        let props = Object.keys(this.data.data);
        let adds = {};
        for (let i = 0; i < props.length; i++) {
            adds[props[i]] = 0;
        }

        let leftPoint = this.data.point - this.data.use;

        
        for (let i = 0; i < leftPoint; i++) {
            let select = Math.floor(Math.random()*props.length);
            adds[props[select]] +=1;
        }

        //赋值
        this.data.use = this.data.point;
        //更新属性
        for (let i = 0; i < props.length; i++) {
            const prop = props[i];
            this.data.add[prop] += adds[prop];
            this.data.data[prop] += adds[prop];
        }
    }

    onClickOk(){
        //结果保护
        if(this.data.use > this.data.point){
            return;
        }

        for (const key in this.data.add) {
            player[key] += this.data.add[key];
        }

        player.prop_point -= this.data.use;

        this.close();
    }

    onClickCancel(){
        this.close();
    }

}